<template>
<lb-picker 
	@confirm="bindPickerChange"
	@columnchange="columnchange" 
	:list="array" 
	:props="{label:'name',value:'name',children:'city'}" 
	:value="value"
	:cancel-text="$t('取消')"
	:confirm-text="$t('确定')"
	mode="multiSelector" 
	:level="3">
    <slot></slot>
</lb-picker>
</template>

<script>
    // import AllAddress from './en.js'
    import br from './br.js'
    import zh_cn from './cn.js'
    import en from './en.js'
    import tl from './tl.js'
    let selectVal = ['','','']
    
    export default {
        data() {
            return{
                value: ['', '', ''],
                array: [],
                index: 0
            }
        },
		props: {
			parentData: Array
		},
        created() {
            this.initSelect();
        },
		watch:{
			parentData(newD, old){
				this.value = [newD[0], newD[1], newD[2]]
			}
		},
        methods:{
            // 初始化地址选项
            initSelect() {
                this.updateSourceDate() // 更新源数据
                .updateAddressDate() // 更新结果数据
                .$forceUpdate()  // 触发双向绑定
            },
            // 地址控件改变控件
            columnchange(d) {
                this.updateSelectIndex(d.detail.column, d.detail.value) // 更新选择索引
                .updateSourceDate() // 更新源数据
                .updateAddressDate() // 更新结果数据
                .$forceUpdate()  // 触发双向绑定
            },
            
            /**
             * 更新源数据
             * */
            updateSourceDate() {
				let AllAddress = "";
				let language = uni.getStorageSync("mhlanguage");
				if (language === "en") {
					AllAddress = en
				} else if (language === "zh-cn") {
					AllAddress = zh_cn
				} else if (language === "br") {
					AllAddress = br
				} else if (language === "tl") {
					AllAddress = tl
				}
				
                this.array = AllAddress
                // this.array[0] = AllAddress.map(obj => {
                //     return {
                //         name: obj.name
                //     }
                // })
                // this.array[1] = AllAddress[this.value[0]].city.map(obj => {
                //     return {
                //         name: obj.name
                //     }
                // })
                // this.array[2] = AllAddress[this.value[0]].city[this.value[1]].city.map(obj => { 
                //     return {
                //         name: obj
                //     }
                // })
                return this
            },
            
            /**
             * 更新索引
             * */
            updateSelectIndex(column, value){
                let arr = JSON.parse(JSON.stringify(this.value)) 
                arr[column] = value
                if(column === 0 ) {
                    arr[1] = 0
                    arr[2] = 0
                }
                if(column === 1 ) {
                    arr[2] = 0
                }
                this.value = arr
                return this
            },
            
            /**
             * 更新结果数据 
             * */
            updateAddressDate() {
				// console.log(this.array)
                selectVal[0] = this.array[0][this.value[0]].name
                selectVal[1] = this.array[1][this.value[1]].name 
                selectVal[2] = this.array[2][this.value[2]].name 
                return this
            },
            
            /**
             * 点击确定
             * */
            bindPickerChange(e) {
				console.log(e)
                this.$emit('change', {
                    index: this.value,
                    data: [e.value[0],e.value[1],e.item[2]]
                })
                return this
            }
            
        }
    }
</script>

<style>
</style>


